<template>
  <div class="employees-container">
    <div class="app-container">
      <!-- 头部卡片公共组件 -->
      <PageTools>
        <template #left>共{{ total }}</template>
        <template #right>
          <el-button size="small" type="danger">excel导入</el-button>
          <el-button size="small" type="warning">excel导出</el-button>
          <el-button size="small" type="primary" @click="showDialog=true">新增员工</el-button>
        </template>
      </PageTools>
      <!-- 员工列表 -->
      <el-card style="margin-top: 10px;">
        <el-table v-loading="loading" :data="list" border stripe>
          <el-table-column label="序号" type="index" :index="indexMethod" sortable="" width="100" />
          <el-table-column label="姓名" prop="username" sortable="" />
          <!-- <el-table-column label="头像" prop="staffPhoto" sortable="" /> -->
          <!-- 头像列最后来处理 -->
          <!-- <el-table-column label="头像" prop="staffPhoto" sortable="" /> -->
          <el-table-column label="手机号" prop="mobile" sortable="" />
          <el-table-column label="工号" prop="workNumber" sortable="" />
          <!-- 如果列添加了作用域插槽  el-table-column上的prop属性不再生效, 以作用域插槽为准 -->
          <!-- <el-table-column label="聘用形式" prop="formOfEmployment" sortable="">
            <template #default="{row}">
              {{ row.formOfEmployment===1?'正式':'非正式' }}
            </template>
          </el-table-column> -->
          <el-table-column label="聘用形式" prop="formOfEmployment" :formatter="formatter" sortable="" />
          <el-table-column label="部门" prop="departmentName" sortable="" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable="" />
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template #default="{row}">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small" @click="delEmployee(row.id)">删除</el-button>
            </template>
          </el-table-column></el-table>
        <!-- 分页组件 -->
        <el-pagination
          style="margin-top: 20px"
          :current-page="page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-card>
      <add-employee :show-dialog.sync="showDialog"></add-employee>
    </div>
  </div>
</template>

<script>
import { reqDelEmployee, reqGetEmployeeList } from '@/api/employees'
import employeeMenu from '@/constant/employees'
import AddEmployee from './components/add-employee'
export default {
  name: 'Employees',
  components: {
    AddEmployee
  },
  data() {
    return {
      list: [], // 员工列表
      total: 0, // 员工总数
      page: 1,
      size: 5,
      showDialog: false // 控制新增员工的对话框的布尔值
    }
  },
  created() {
    this.getEmployees()
  },
  methods: {
    async getEmployees() {
      this.loading = true
      const { data: { rows, total }} = await reqGetEmployeeList(this.page, this.size)
      this.list = rows
      this.total = total
      this.loading = false
    },
    // size发送变化触发的方法
    handleSizeChange(newVal) {
      this.size = newVal
      this.page = 1
      this.getEmployees()
    },
    // 页码发送变化触发的方法
    handleCurrentChange(newVal) {
      this.page = newVal
      this.getEmployees()
    },
    // 自定义索引列
    indexMethod(index) {
      return index + 1 + (this.page - 1) * this.size
    },
    // 聘用形式格式化列的方法
    formatter(row, column, cellValue, index) {
      // row 当前行的信息
      // column 是当前列的信息
      // cellValue 单元格的值
      // index 索引
      console.log(row, column, cellValue, index)
      // return cellValue === 1 ? '正式员工' : '非正式员工'
      // console.log(employeeMenu.hireType)
      // return row.formOfEmployment === 1 ? '正式员工' : '非正式员工'
      // return cellValue === 1 ? '正式员工' : '非正式员工'
      const obj = employeeMenu.hireType.find(item => item.id === cellValue)
      return obj ? obj.value : '黑户'
    },
    // 删除
    delEmployee(id) {
      // 确认消息的提示
      this.$confirm('此操作将永久删除该员工, 是否继续?', '提示', {
        type: 'warning'
      }).then(async() => {
        // 发送请求
        const { message } = await reqDelEmployee(id)
        // 提示用户
        this.$message.success(message)
        // bug page--
        if (this.list.length === 1 && this.size > 1) {
          this.page--
        }
        // 重新渲染
        this.getEmployees()
      }).catch(() => {
        this.$message.info('删除失败')
      })
    }
  }
}
</script>

<style>

</style>
